<?php
 class ApiCenter_ProductApi_Model_Api2_Product_Image_Rest extends ApiCenter_ProductApi_Model_Api2_Product_Image{
     
     const ATTRIBUTE_CODE = 'media_gallery'; 
     
     protected $_mimeTypes = array(
         'image/jpeg' => 'jpg',
         'image/gif'  => 'gif',
         'image/png'  => 'png'
     );
     
     /**
      * 上传图片
      * @param $product Mage_Catalog_Model_Product
      * @param $data 图片数据
      * @return $files
      **/
     protected function uploadImage($product,$data) {

         $gallery = $this->_getGalleryAttribute($product);
         
         
         if ( !isset($data['mime']) || !isset($data['content'])) {
             $this->_critical(Mage::helper('catalog')->__('The image is not specified.'));
         }
     
         if (!isset($this->_mimeTypes[$data['mime']])) {
             $this->_critical( Mage::helper('catalog')->__('Invalid image type.'));
         }
     
         $fileContent = @base64_decode($data['content'], true);
         
         if (!$fileContent) {
             $this->_critical(Mage::helper('catalog')->__('The image contents is not valid base64 data.'));
         }
     
         unset($data['content']);
     
         $pram = date("YmdHis");
         $tmpDirectory = Mage::getBaseDir('var') . DS . 'api' . DS . $pram;

         
         if (isset($data['name']) && $data['name']) {
             $fileName  = $data['name'];
         } else {
             $fileName  = 'images';
         }
            $fileName .= '.' . $this->_mimeTypes[$data['mime']];

         $ioAdapter = new Varien_Io_File();
         try {
             // Create temporary directory for api
             $ioAdapter->checkAndCreateFolder($tmpDirectory);
             $ioAdapter->open(array('path'=>$tmpDirectory));
             // Write image file

              $ioAdapter->write($fileName, $fileContent, 0777);
                         
            unset($fileContent);
     
             // try to create Image object - it fails with Exception if image is not supported
             try {
                 new Varien_Image($tmpDirectory . DS . $fileName);
             } catch (Exception $e) {
                 // Remove temporary directory
                 $ioAdapter->rmdir($tmpDirectory, true);
                 throw new Mage_Core_Exception($e->getMessage());
             }
             // Adding image to gallery
             $file = $gallery->getBackend()->addImage(
                 $product,
                 $tmpDirectory . DS . $fileName,
                 null,
                 false,
                 false
             );
           
             // Remove temporary directory
            $ioAdapter->rmdir($tmpDirectory, true);
     
           //  $gallery->getBackend()->updateImage($product, $file, $data);
             
//              if (isset($data['types'])) {
//                  $gallery->getBackend()->setMediaAttribute($product, $data['types'], $file);
//              }
     
         } catch (Mage_Core_Exception $e) {
             $this->_critical($e->getMessage());
         } catch (Exception $e) {
             $this->_critical(Mage::helper('catalog')->__('Cannot create image.'));
         }
         return $gallery->getBackend()->getRenamedImage($file);
     }
     
     protected function _getGalleryAttribute($product)
     {
         $attributes = $product->getTypeInstance(true)
         ->getSetAttributes($product);
     
         if (!isset($attributes[self::ATTRIBUTE_CODE])) {
             $this->_critical('not_media');
         }
     
         return $attributes[self::ATTRIBUTE_CODE];
     }
 }